﻿using Common;
using gregn6Lib;
using NursingFormSystem.Common;
using NursingFormSystem.Model;
using SQLDLL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NursingFormSystem.KnowingAgreeBook
{
    public partial class AgreeBookPrints : Form
    {
        private GridppReport Report = new GridppReport();
        NurFormsService.NursingForm formService = new NurFormsService.NursingForm();
        PatientInfo PatientInfo;      
        int FormID = 0;
        DateTime Start;
        DateTime End;
        string Id = string.Empty;
        Tb_User LoginUser;
        ElectronicSignature es;
        /// <summary>
        /// 单张或多张打印
        /// </summary>
        /// <param name="loginUser">登录信息</param>
        /// <param name="patientInfo">患者信息</param>
        /// <param name="id">id</param>
        public AgreeBookPrints(Tb_User loginUser, PatientInfo patientInfo, string id)
        {
            InitializeComponent();
            PatientInfo = patientInfo;
            LoginUser = loginUser;
            this.Id = id;
            es = new ElectronicSignature(this.LoginUser);
            Report.LoadFromFile(System.Windows.Forms.Application.StartupPath + @"\GRFModel\Ydhl_GRF\特殊护理措施知情同意书.grf");
            Report.ConnectionString = Utility.GetDatabaseConnectionString();
            Report.FetchRecord += new _IGridppReportEvents_FetchRecordEventHandler(ReportFetchRecord);
            axGRPrintViewer1.Report = Report;
        }

        private void ReportFetchRecord()
        {
            var ret = formService.GetNursingFormRecord(PatientInfo.PATIENT_ID, PatientInfo.VISIT_ID, (int)FormType.Knowing);
            DataTable dt = new DataTable();
            if (ret.Code == 1)
            {
                CreateDataTableHead(dt);
                List<NursingFormRecord> list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<NursingFormRecord>>(ret.D);
                if (!string.IsNullOrEmpty(this.Id))
                    list = list.Where(p => p.Id.Equals(Convert.ToInt32(this.Id))).ToList();
                for (int i = 0; i < list.Count; i++)
                {
                    NursingFormRecord record = list[i];
                    Knowing ps = XmlUtil.XMLToObject<Knowing>(record.NursingFormData, Encoding.UTF8);
                    string image1 = "";
                    string RYHS = ps.HuShiQianMing;
                    if (!string.IsNullOrEmpty(RYHS))
                    {
                        image1 = es.GetElectricSignBase64(record.AreaId, ps.HuShiQianMing);
                    }
                    dt.Rows.Add(
                        PatientInfo.NURSE_CELL_NAME,//NURSE_CELL_NAME,
                        PatientInfo.BED_NO,//BED_NO,
                        PatientInfo.PATIENT_NAME,
                        PatientInfo.SEX,//SEX
                        PatientInfo.AGE,//AGE
                        PatientInfo.PATIENT_ID,//PATIENTID

                        ChkStr(ps.YueShuGongJu.YuanYin, 4) && ps.YueShuGongJu.YuanYin.Substring(0, 1).Equals("1") ? 1 : 0,
                        ChkStr(ps.YueShuGongJu.YuanYin, 4) && ps.YueShuGongJu.YuanYin.Substring(2, 1).Equals("1") ? 1 : 0,
                        ChkStr(ps.YueShuGongJu.YuanYin, 4) && ps.YueShuGongJu.YuanYin.Substring(4, 1).Equals("1") ? 1 : 0,
                        ChkStr(ps.YueShuGongJu.YuanYin, 4) && ps.YueShuGongJu.YuanYin.Substring(6, 1).Equals("1") ? 1 : 0,
                        ps.YueShuGongJu.YY_QiTa,

                        ChkStr(ps.YueShuGongJu.YuFang, 5) && ps.YueShuGongJu.YuFang.Substring(0, 1).Equals("1") ? 1 : 0,
                        ChkStr(ps.YueShuGongJu.YuFang, 5) && ps.YueShuGongJu.YuFang.Substring(2, 1).Equals("1") ? 1 : 0,
                        ChkStr(ps.YueShuGongJu.YuFang, 5) && ps.YueShuGongJu.YuFang.Substring(4, 1).Equals("1") ? 1 : 0,
                        ChkStr(ps.YueShuGongJu.YuFang, 5) && ps.YueShuGongJu.YuFang.Substring(6, 1).Equals("1") ? 1 : 0,
                        ChkStr(ps.YueShuGongJu.YuFang, 5) && ps.YueShuGongJu.YuFang.Substring(8, 1).Equals("1") ? 1 : 0,
                        ps.YueShuGongJu.YF_QiTa,

                        ChkStr(ps.YueShuGongJu.BingFaZheng, 4) && ps.YueShuGongJu.BingFaZheng.Substring(0, 1).Equals("1") ? 1 : 0,
                        ChkStr(ps.YueShuGongJu.BingFaZheng, 4) && ps.YueShuGongJu.BingFaZheng.Substring(2, 1).Equals("1") ? 1 : 0,
                        ChkStr(ps.YueShuGongJu.BingFaZheng, 4) && ps.YueShuGongJu.BingFaZheng.Substring(4, 1).Equals("1") ? 1 : 0,
                        ChkStr(ps.YueShuGongJu.BingFaZheng, 4) && ps.YueShuGongJu.BingFaZheng.Substring(6, 1).Equals("1") ? 1 : 0,
                        ps.YueShuGongJu.BFZ_QiTa,

                        ChkStr(ps.ErShiSiXiaoShiKanHu.YiShiZhangAi, 4) && ps.ErShiSiXiaoShiKanHu.YiShiZhangAi.Substring(0, 1).Equals("1") ? 1 : 0,
                        ChkStr(ps.ErShiSiXiaoShiKanHu.YiShiZhangAi, 4) && ps.ErShiSiXiaoShiKanHu.YiShiZhangAi.Substring(2, 1).Equals("1") ? 1 : 0,
                        ChkStr(ps.ErShiSiXiaoShiKanHu.YiShiZhangAi, 4) && ps.ErShiSiXiaoShiKanHu.YiShiZhangAi.Substring(4, 1).Equals("1") ? 1 : 0,
                        ChkStr(ps.ErShiSiXiaoShiKanHu.YiShiZhangAi, 4) && ps.ErShiSiXiaoShiKanHu.YiShiZhangAi.Substring(6, 1).Equals("1") ? 1 : 0,
                        ps.ErShiSiXiaoShiKanHu.YSZA_QiTa,

                        ChkStr(ps.ErShiSiXiaoShiKanHu.JingShenZhangAi, 3) && ps.ErShiSiXiaoShiKanHu.JingShenZhangAi.Substring(0, 1).Equals("1") ? 1 : 0,
                        ChkStr(ps.ErShiSiXiaoShiKanHu.JingShenZhangAi, 3) && ps.ErShiSiXiaoShiKanHu.JingShenZhangAi.Substring(2, 1).Equals("1") ? 1 : 0,
                        ChkStr(ps.ErShiSiXiaoShiKanHu.JingShenZhangAi, 3) && ps.ErShiSiXiaoShiKanHu.JingShenZhangAi.Substring(4, 1).Equals("1") ? 1 : 0,
                        ps.ErShiSiXiaoShiKanHu.JSZA_QiTa,

                        ChkStr(ps.ErShiSiXiaoShiKanHu.NianLin, 2) && ps.ErShiSiXiaoShiKanHu.NianLin.Substring(0, 1).Equals("1") ? 1 : 0,
                        ChkStr(ps.ErShiSiXiaoShiKanHu.NianLin, 2) && ps.ErShiSiXiaoShiKanHu.NianLin.Substring(2, 1).Equals("1") ? 1 : 0,
                        ps.ErShiSiXiaoShiKanHu.NL_QiTa,

                        ChkStr(ps.ErShiSiXiaoShiKanHu.YuFang, 6) && ps.ErShiSiXiaoShiKanHu.YuFang.Substring(0, 1).Equals("1") ? 1 : 0,
                        ChkStr(ps.ErShiSiXiaoShiKanHu.YuFang, 6) && ps.ErShiSiXiaoShiKanHu.YuFang.Substring(2, 1).Equals("1") ? 1 : 0,
                        ChkStr(ps.ErShiSiXiaoShiKanHu.YuFang, 6) && ps.ErShiSiXiaoShiKanHu.YuFang.Substring(4, 1).Equals("1") ? 1 : 0,
                        ChkStr(ps.ErShiSiXiaoShiKanHu.YuFang, 6) && ps.ErShiSiXiaoShiKanHu.YuFang.Substring(6, 1).Equals("1") ? 1 : 0,
                        ChkStr(ps.ErShiSiXiaoShiKanHu.YuFang, 6) && ps.ErShiSiXiaoShiKanHu.YuFang.Substring(8, 1).Equals("1") ? 1 : 0,
                        ChkStr(ps.ErShiSiXiaoShiKanHu.YuFang, 6) && ps.ErShiSiXiaoShiKanHu.YuFang.Substring(10, 1).Equals("1") ? 1 : 0,
                        ps.ErShiSiXiaoShiKanHu.YF_QiTa,

                        ps.JiaShuYiJian,
                        ps.QianMing,
                        ps.YuHuanZheGuanXi,
                        !Base.ESSwitch.Equals("on") ? ps.HuShiQianMing:"",
                        ps.RiQi,
                        image1
                        );
                }
                Utility.FillRecordToReport(Report, dt);
            }
            else if (ret.Code == 2)
            {
                CreateDataTableHead(dt);
                DataRow dr = dt.NewRow();
                dt.Rows.Add(dr);
                Utility.FillRecordToReport(Report, dt);
            }
            else if (ret.Code == 0)
            {
                MessageBox.Show(ret.M);
            }
        }
        /// <summary>
        /// 加载列头
        /// </summary>
        /// <param name="dt"></param>
        private static void CreateDataTableHead(DataTable dt)
        {
            dt.Columns.Add("NURSE_CELL_NAME", typeof(string));
            dt.Columns.Add("BED_NO", typeof(string));
            dt.Columns.Add("PATIENT_NAME", typeof(string));
            dt.Columns.Add("SEX", typeof(string));
            dt.Columns.Add("AGE", typeof(string));
            dt.Columns.Add("PATIENT_ID", typeof(string));

            dt.Columns.Add("TSGJ_YuanYin1", typeof(string));
            dt.Columns.Add("TSGJ_YuanYin2", typeof(string));
            dt.Columns.Add("TSGJ_YuanYin3", typeof(string));
            dt.Columns.Add("TSGJ_YuanYin4", typeof(string));
            dt.Columns.Add("TSGJ_YuanYinQT", typeof(string));

            dt.Columns.Add("TSGJ_YuFang1", typeof(string));
            dt.Columns.Add("TSGJ_YuFang2", typeof(string));
            dt.Columns.Add("TSGJ_YuFang3", typeof(string));
            dt.Columns.Add("TSGJ_YuFang4", typeof(string));
            dt.Columns.Add("TSGJ_YuFang5", typeof(string));
            dt.Columns.Add("TSGJ_YuFangQT", typeof(string));

            dt.Columns.Add("TSGJ_BingFaZheng1", typeof(string));
            dt.Columns.Add("TSGJ_BingFaZheng2", typeof(string));
            dt.Columns.Add("TSGJ_BingFaZheng3", typeof(string));
            dt.Columns.Add("TSGJ_BingFaZheng4", typeof(string));
            dt.Columns.Add("TSGJ_BingFaZhengQT", typeof(string));

            dt.Columns.Add("KH_YSZA1", typeof(string));
            dt.Columns.Add("KH_YSZA2", typeof(string));
            dt.Columns.Add("KH_YSZA3", typeof(string));
            dt.Columns.Add("KH_YSZA4", typeof(string));
            dt.Columns.Add("KH_YSZAQT", typeof(string));

            dt.Columns.Add("KH_JSZA1", typeof(string));
            dt.Columns.Add("KH_JSZA2", typeof(string));
            dt.Columns.Add("KH_JSZA3", typeof(string));
            dt.Columns.Add("KH_JSZAQT", typeof(string));

            dt.Columns.Add("KH_NianLin1", typeof(string));
            dt.Columns.Add("KH_NianLin2", typeof(string));
            dt.Columns.Add("KH_NianLinQT", typeof(string));

            dt.Columns.Add("KH_YuFang1", typeof(string));
            dt.Columns.Add("KH_YuFang2", typeof(string));
            dt.Columns.Add("KH_YuFang3", typeof(string));
            dt.Columns.Add("KH_YuFang4", typeof(string));
            dt.Columns.Add("KH_YuFang5", typeof(string));
            dt.Columns.Add("KH_YuFang6", typeof(string));
            dt.Columns.Add("KH_YuFangQT", typeof(string));

            dt.Columns.Add("JiaShuYiJian", typeof(string));
            dt.Columns.Add("QianMing", typeof(string));
            dt.Columns.Add("YuHuanZheGuanXi", typeof(string));
            dt.Columns.Add("HuShiQianMing", typeof(string));
            dt.Columns.Add("RiQi", typeof(string));
            dt.Columns.Add("ElectricSignBase64", typeof(string));//电签
        }
        /// <summary>
        /// 检查数据项目
        /// </summary>
        /// <param name="str"></param>
        /// <param name="len"></param>
        /// <returns></returns>
        private bool ChkStr(string str, int len)
        {
            if (string.IsNullOrEmpty(str)) return false;
            if (str.IndexOf("#") < 0) return false;
            if (str.Split('#').Length < len) return false;
            return true;
        }
        private void AgreeBookPrints_Load(object sender, EventArgs e)
        {
            axGRPrintViewer1.Start();
        }
    }
}
